package edu.gatech.volt2.drdoommgt.gui;

import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextPane;

import edu.gatech.volt2.drdoommgt.domain.Lab;

public class LabworkView extends JFrame implements ILabworkView{
	private JPanel contentPane;
	private JButton addButton;
	private JButton cancelButton;
	private JComboBox whenMonth;
	private JComboBox whenYear;
	private JComboBox whenDay;
	private JTextPane results;
	private JComboBox labSelect;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PatientInvoice frame = new PatientInvoice();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LabworkView() {
		setTitle("Add Labwork");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 350, 251);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SpringLayout sl_contentPane = new SpringLayout();
		contentPane.setLayout(sl_contentPane);
		
		JLabel lblPatient = new JLabel("Lab:");
		contentPane.add(lblPatient);
		
		JLabel lblDateOfVisit = new JLabel("Results:");
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblDateOfVisit, 15, SpringLayout.SOUTH, lblPatient);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblDateOfVisit, 6, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, lblPatient, 0, SpringLayout.EAST, lblDateOfVisit);
		contentPane.add(lblDateOfVisit);
		
		addButton = new JButton("Add");
		sl_contentPane.putConstraint(SpringLayout.EAST, addButton, 0, SpringLayout.EAST, contentPane);
		contentPane.add(addButton);
		
		cancelButton = new JButton("Cancel");
		sl_contentPane.putConstraint(SpringLayout.NORTH, addButton, 0, SpringLayout.NORTH, cancelButton);
		sl_contentPane.putConstraint(SpringLayout.WEST, cancelButton, 10, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, cancelButton, 0, SpringLayout.SOUTH, contentPane);
		contentPane.add(cancelButton);
		
		labSelect = new JComboBox();
		labSelect.setEnabled(false);
		sl_contentPane.putConstraint(SpringLayout.WEST, labSelect, 15, SpringLayout.EAST, lblPatient);
		sl_contentPane.putConstraint(SpringLayout.EAST, labSelect, -10, SpringLayout.EAST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblPatient, 5, SpringLayout.NORTH, labSelect);
		sl_contentPane.putConstraint(SpringLayout.NORTH, labSelect, 17, SpringLayout.NORTH, contentPane);
		contentPane.add(labSelect);
		
		results = new JTextPane();
		sl_contentPane.putConstraint(SpringLayout.NORTH, results, 0, SpringLayout.NORTH, lblDateOfVisit);
		sl_contentPane.putConstraint(SpringLayout.WEST, results, 76, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.SOUTH, results, -51, SpringLayout.NORTH, addButton);
		sl_contentPane.putConstraint(SpringLayout.EAST, results, -10, SpringLayout.EAST, contentPane);
		contentPane.add(results);
		
		whenDay = new JComboBox();
		sl_contentPane.putConstraint(SpringLayout.NORTH, whenDay, 18, SpringLayout.SOUTH, results);
		sl_contentPane.putConstraint(SpringLayout.WEST, whenDay, 152, SpringLayout.WEST, contentPane);
		contentPane.add(whenDay);
		
		whenYear = new JComboBox();
		sl_contentPane.putConstraint(SpringLayout.WEST, whenYear, 232, SpringLayout.WEST, contentPane);
		sl_contentPane.putConstraint(SpringLayout.EAST, whenDay, -5, SpringLayout.WEST, whenYear);
		sl_contentPane.putConstraint(SpringLayout.NORTH, whenYear, 0, SpringLayout.NORTH, whenDay);
		sl_contentPane.putConstraint(SpringLayout.EAST, whenYear, -12, SpringLayout.EAST, labSelect);
		contentPane.add(whenYear);
		
		whenMonth = new JComboBox();
		sl_contentPane.putConstraint(SpringLayout.NORTH, whenMonth, 0, SpringLayout.NORTH, whenDay);
		sl_contentPane.putConstraint(SpringLayout.EAST, whenMonth, -6, SpringLayout.WEST, whenDay);
		contentPane.add(whenMonth);
		
		JLabel lblLabOn = new JLabel("Lab On:");
		sl_contentPane.putConstraint(SpringLayout.WEST, whenMonth, 17, SpringLayout.EAST, lblLabOn);
		sl_contentPane.putConstraint(SpringLayout.NORTH, lblLabOn, 87, SpringLayout.SOUTH, lblDateOfVisit);
		sl_contentPane.putConstraint(SpringLayout.WEST, lblLabOn, 0, SpringLayout.WEST, lblDateOfVisit);
		contentPane.add(lblLabOn);
		
		this.setCalOptions();
	}
	
	private void setCalOptions(){
		Calendar cal = Calendar.getInstance();
		for ( int month=1;month<=12;month++ ){
			whenMonth.addItem(month);
			if( month == (cal.get(Calendar.MONTH)+1)){
				whenMonth.setSelectedItem(month);
			}
		}
		for ( int days=1;days<=31;days++ ){
			whenDay.addItem(days);
			if( days == (cal.get(Calendar.DAY_OF_MONTH))){
				whenDay.setSelectedItem(days);
			}
		}
		for (int year=cal.get(Calendar.YEAR);year<(cal.get(Calendar.YEAR)+2);year++){
			whenYear.addItem(year);
		}
	}
	
	public void setLabOptions(List<Lab> labs){
		for( Lab l : labs ){
			labSelect.addItem(l);
		}
	}
	
	public void enableLabSelect(){
		labSelect.setEnabled(true);
	}

	@Override
	public void showSelf() {
		this.setVisible(true);
	}

	@Override
	public void addListener(ActionListener al) {
		addButton.addActionListener(al);
		cancelButton.addActionListener(al);
	}

	@Override
	public void hideSelf() {
		this.setVisible(false);		
	}

	@Override
	public JButton getCancelButton() {
		return cancelButton;
	}

	@Override
	public JButton getAddButton() {
		return addButton;
	}

	@Override
	public Lab getLab() {
		return (Lab) labSelect.getSelectedItem();
	}

	@Override
	public Calendar getDate() {
		Calendar cal = Calendar.getInstance();
		int year = Integer.parseInt(whenYear.getSelectedItem().toString());
		int month = Integer.parseInt(whenMonth.getSelectedItem().toString());
		int date = Integer.parseInt(whenDay.getSelectedItem().toString());
		cal.set(year, month, date);
		return cal;
	}

	@Override
	public String getResults() {
		return results.getText();
	}
	
	
	
}
